
# --- Libraries --- #
library(countrycode)
library(xergm)
set.seed(1912)

setwd("YOUR WORKING DIRECTORY")



# --- Pauls and Cranmer Data --- #

mid.nw <- readRDS("data/pauls_cranmer/original/mid_nw.rds") # conflict onset
directcont.nw <- readRDS("data/pauls_cranmer/original/directcont_nw.rds")  # contiguity
capratio <- readRDS("data/pauls_cranmer/original/capratio.rds")  # capabilities ratio
tradedep <- readRDS("data/pauls_cranmer/original/tradedep.rds") # trade dependence
igosec <- readRDS("data/pauls_cranmer/original/igosec.rds")  # security IGOs
igoecon <- readRDS("data/pauls_cranmer/original/igoecon.rds")  # econ IGOs 






# --- Adjust time range
mid.nw_full <- mid.nw[7:26] # Start these at 1971 in order to lag votes/speeches by t-1 = 1970; index7=1971, index26=1990
directcont.nw_full <- directcont.nw[7:26]
capratio_full <- capratio[7:26]
tradedep_full <- tradedep[7:26]
igosec_full <- igosec[7:26]
igoecon_full <- igoecon[7:26]




# --- Convert country names to iso3c (to match name format of multiplex data; also slightly easier to interpret):
custom_match <- c(`315` = "CSK", `265` = "DDR", `680` = "YDYE", `345` = "YUG", `816` = "SVM",
                  `999` = "HKG", `998` = "COK", `260` = "DEU", `678` = "YAR")

for(i in 1:length(mid.nw_full)){
  
  t <- countrycode(network::get.vertex.attribute(mid.nw_full[[i]], "vertex.names"), "cown", "iso3c", custom_match = custom_match)
  network::set.vertex.attribute(mid.nw_full[[i]], "vertex.names", t)
  adj.i <- mid.nw_full[[i]][,]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  ifelse(dim(mid.nw_full[[i]][,])==dim(adj.i),  network::set.vertex.attribute(net.i, "democ", network::get.vertex.attribute(mid.nw_full[[i]], "democ")),
         network::set.vertex.attribute(net.i, "democ", network::get.vertex.attribute(mid.nw_full[[i]], "democ")[-which(duplicated(rownames(mid.nw_full[[i]][,])))]))
  mid.nw_full[[i]] <- net.i
  
  t <- countrycode(network::get.vertex.attribute(directcont.nw_full[[i]], "vertex.names"), "cown", "iso3c", custom_match = custom_match)
  network::set.vertex.attribute(directcont.nw_full[[i]], "vertex.names", t)
  adj.i <- directcont.nw_full[[i]][,]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  directcont.nw_full[[i]] <- net.i
  
  t <- countrycode(rownames(capratio_full[[i]]), "cown", "iso3c", custom_match = custom_match)
  rownames(capratio_full[[i]]) <- t
  colnames(capratio_full[[i]]) <- t
  capratio_full[[i]] <- capratio_full[[i]][!duplicated(rownames(capratio_full[[i]])),!duplicated(colnames(capratio_full[[i]]))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  t <- countrycode(rownames(tradedep_full[[i]]), "cown", "iso3c", custom_match = custom_match)
  rownames(tradedep_full[[i]]) <- t
  colnames(tradedep_full[[i]]) <- t
  tradedep_full[[i]] <- tradedep_full[[i]][!duplicated(rownames(tradedep_full[[i]])),!duplicated(colnames(tradedep_full[[i]]))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  t <- countrycode(rownames(igosec_full[[i]]), "cown", "iso3c", custom_match = custom_match)
  rownames(igosec_full[[i]]) <- t
  colnames(igosec_full[[i]]) <- t
  igosec_full[[i]] <- igosec_full[[i]][!duplicated(rownames(igosec_full[[i]])),!duplicated(colnames(igosec_full[[i]]))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  t <- countrycode(rownames(igoecon_full[[i]]), "cown", "iso3c", custom_match = custom_match)
  rownames(igoecon_full[[i]]) <- t
  colnames(igoecon_full[[i]]) <- t
  igoecon_full[[i]] <- igoecon_full[[i]][!duplicated(rownames(igoecon_full[[i]])),!duplicated(colnames(igoecon_full[[i]]))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  
}


# --- Save these down
# Only changes vs. original data here are (1) subsetting time range to match our range, and (2) altering names from cown --> iso3c. 
# These represent a system level view of confict and predictors

saveRDS(mid.nw_full, file = "data/pauls_cranmer/prepped/system/mid.nw_full.rds")
saveRDS(directcont.nw_full, file = "data/pauls_cranmer/prepped/system/directcont.nw_full.rds") 
saveRDS(capratio_full, file = "data/pauls_cranmer/prepped/system/capratio_full.rds") 
saveRDS(tradedep_full, file = "data/pauls_cranmer/prepped/system/tradedep_full.rds") 
saveRDS(igosec_full, file = "data/pauls_cranmer/prepped/system/igosec_full.rds") 
saveRDS(igoecon_full, file = "data/pauls_cranmer/prepped/system/igoecon_full.rds") 

 
 






# --- Subsetted networks --- #
# Next, subset network to define node set based on whether or not node ever appears in a community
# This provides a more granular snapshot of what's going on in the communities, vs. the entire system

mid.nw_full <- readRDS("data/pauls_cranmer/prepped/system/mid.nw_full.rds")
directcont.nw_full <- readRDS("data/pauls_cranmer/prepped/system/directcont.nw_full.rds") 
capratio_full <- readRDS("data/pauls_cranmer/prepped/system/capratio_full.rds") 
tradedep_full <- readRDS("data/pauls_cranmer/prepped/system/tradedep_full.rds") 
igosec_full <- readRDS("data/pauls_cranmer/prepped/system/igosec_full.rds") 
igoecon_full <- readRDS("data/pauls_cranmer/prepped/system/igoecon_full.rds") 



# --- Weak signal subsets:
# The weak node set is constant across parameter settings, so import any of the lists to pull out the required names

multi_comms_i.adj <- readRDS("data/comm_detection/detected_comms/weak/weak_ties_100d_15x.rds")[1970:1989] 


mid_sub <- list()
directcont_sub <- list()
capratio_sub <- list()
tradedep_sub <- list()
igosec_sub <- list()
igoecon_sub <- list()
for(i in 1:length(mid.nw_full)){
  
  # conflict
  adj.i <- mid.nw_full[[i]][,][rownames(mid.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                               colnames(mid.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  df.temp <- subset(data.frame(democ = network::get.vertex.attribute(mid.nw_full[[i]], "democ"), 
                               names = network::get.vertex.attribute(mid.nw_full[[i]], "vertex.names")), names %in% rownames(adj.i))
  network::set.vertex.attribute(net.i, "democ", df.temp$democ)
  
  mid_sub[[i]] <- net.i
  
  
  # contig
  adj.i <- directcont.nw_full[[i]][,][rownames(directcont.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                                      colnames(directcont.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  
  directcont_sub[[i]] <- net.i
  
  # cap ratio
  adj.i <- capratio_full[[i]][rownames(capratio_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(capratio_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  capratio_sub[[i]] <- adj.i
  
  #trade dep
  adj.i <- tradedep_full[[i]][rownames(tradedep_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(tradedep_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  tradedep_sub[[i]] <- adj.i
  
  #igo sec
  adj.i <- igosec_full[[i]][rownames(igosec_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                            colnames(igosec_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igosec_sub[[i]] <- adj.i
  
  #igo econ
  adj.i <- igoecon_full[[i]][rownames(igoecon_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                             colnames(igoecon_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igoecon_sub[[i]] <- adj.i
  
}

saveRDS(directcont_sub, file = "data/pauls_cranmer/prepped/subsets/weak_nodes/predictors/directcont_sub_w.rds")
saveRDS(capratio_sub, file = "data/pauls_cranmer/prepped/subsets/weak_nodes/predictors/capratio_sub_w.rds")
saveRDS(tradedep_sub, file = "data/pauls_cranmer/prepped/subsets/weak_nodes/predictors/tradedep_sub_w.rds")
saveRDS(igosec_sub, file = "data/pauls_cranmer/prepped/subsets/weak_nodes/predictors/igosec_sub_w.rds")
saveRDS(igoecon_sub, file = "data/pauls_cranmer/prepped/subsets/weak_nodes/predictors/igoecon_sub_w.rds")
 












# --- Add bridge/membership covariate to outcome nets (for the different embeddings)

dir <- "data/comm_detection/detected_comms/weak/"
file_list <- list.files(dir, pattern = "weak_membs_")


for(k in 1:length(file_list)){

  file.i <- file_list[[k]]
  
  print(k)
  
  membs <- readRDS(paste(dir, file.i, sep = ""))[1970:1989]
  

for(i in 1:length(mid_sub)){

  names <- network::get.vertex.attribute(mid_sub[[i]], "vertex.names")
  df <- data.frame(bridge = rep(0, length(names)), row.names = names)
  
  temp <- as.data.frame(membs[[i]]) 

  for(j in 1:nrow(df)){
    if(rownames(df)[j] %in% rownames(temp)){
      temp.i <- subset(temp, rownames(temp) == rownames(df)[j])
      df[j,] <- ifelse(rowSums(temp.i)>1, 1, 0)
    }else{}
  }

  df2 <- data.frame(comm = rep(0, length(names)), row.names = names)
  for(z in 1:nrow(df2)){
    if(rownames(df2)[z] %in% rownames(temp)){
      temp.i <- subset(temp, rownames(temp) == rownames(df2)[z])
      df2[z,] <- ifelse(rowSums(temp.i)==1,as.numeric(colnames(temp.i)[apply(temp.i,1,which.max)]), 0)
    }else{}
  }
  
  network::set.vertex.attribute(mid_sub[[i]], "weak_bridge", df[,1]) # bridge across at least two communities
  network::set.vertex.attribute(mid_sub[[i]], "weak_comm", df2[,1]) # comm assignment if in only one comm
}
  
  saveRDS(mid_sub, file = paste("data/pauls_cranmer/prepped/subsets/weak_nodes/outcome_nets/mid_sub_w_",gsub("weak_membs_", "", file.i), sep = ""))
  
}















# --- Strong signal subsets --- #

# --- Master node set:

multi_comms_i.adj <- readRDS("data/comm_detection/detected_comms/strong/strong_ties_masternodes_prop0.2.rds")[1971:1990] 



mid_sub <- list()
directcont_sub <- list()
capratio_sub <- list()
tradedep_sub <- list()
igosec_sub <- list()
igoecon_sub <- list()
for(i in 1:length(mid.nw_full)){
  
  # conflict
  adj.i <- mid.nw_full[[i]][,][rownames(mid.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                               colnames(mid.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  df.temp <- subset(data.frame(democ = network::get.vertex.attribute(mid.nw_full[[i]], "democ"), 
                               names = network::get.vertex.attribute(mid.nw_full[[i]], "vertex.names")), names %in% rownames(adj.i))
  network::set.vertex.attribute(net.i, "democ", df.temp$democ)
  
  mid_sub[[i]] <- net.i
  
  
  # contig
  adj.i <- directcont.nw_full[[i]][,][rownames(directcont.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                                      colnames(directcont.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  
  directcont_sub[[i]] <- net.i
  
  # cap ratio
  adj.i <- capratio_full[[i]][rownames(capratio_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(capratio_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  capratio_sub[[i]] <- adj.i
  
  #trade dep
  adj.i <- tradedep_full[[i]][rownames(tradedep_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(tradedep_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  tradedep_sub[[i]] <- adj.i
  
  #igo sec
  adj.i <- igosec_full[[i]][rownames(igosec_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                            colnames(igosec_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igosec_sub[[i]] <- adj.i
  
  #igo econ
  adj.i <- igoecon_full[[i]][rownames(igoecon_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                             colnames(igoecon_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igoecon_sub[[i]] <- adj.i
  
}

saveRDS(directcont_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_master/directcont_sub_s_masternodes.rds")
saveRDS(capratio_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_master/capratio_sub_s_masternodes.rds")
saveRDS(tradedep_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_master/tradedep_sub_s_masternodes.rds")
saveRDS(igosec_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_master/igosec_sub_s_masternodes.rds")
saveRDS(igoecon_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_master/igoecon_sub_s_masternodes.rds")








# --- Weak node set:

multi_comms_i.adj <- readRDS("data/comm_detection/detected_comms/strong/strong_ties_weaknodes_prop0.2.rds")[1971:1990] 


mid_sub <- list()
directcont_sub <- list()
capratio_sub <- list()
tradedep_sub <- list()
igosec_sub <- list()
igoecon_sub <- list()
for(i in 1:length(mid.nw_full)){
  
  # conflict
  adj.i <- mid.nw_full[[i]][,][rownames(mid.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                               colnames(mid.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  df.temp <- subset(data.frame(democ = network::get.vertex.attribute(mid.nw_full[[i]], "democ"), 
                               names = network::get.vertex.attribute(mid.nw_full[[i]], "vertex.names")), names %in% rownames(adj.i))
  network::set.vertex.attribute(net.i, "democ", df.temp$democ)
  
  mid_sub[[i]] <- net.i
  
  
  # contig
  adj.i <- directcont.nw_full[[i]][,][rownames(directcont.nw_full[[i]][,]) %in% rownames(multi_comms_i.adj[[i]]),
                                      colnames(directcont.nw_full[[i]][,]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  net.i <- network(adj.i, directed = F)
  
  directcont_sub[[i]] <- net.i
  
  # cap ratio
  adj.i <- capratio_full[[i]][rownames(capratio_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(capratio_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  capratio_sub[[i]] <- adj.i
  
  #trade dep
  adj.i <- tradedep_full[[i]][rownames(tradedep_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                              colnames(tradedep_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  tradedep_sub[[i]] <- adj.i
  
  #igo sec
  adj.i <- igosec_full[[i]][rownames(igosec_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                            colnames(igosec_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igosec_sub[[i]] <- adj.i
  
  #igo econ
  adj.i <- igoecon_full[[i]][rownames(igoecon_full[[i]]) %in% rownames(multi_comms_i.adj[[i]]),
                             colnames(igoecon_full[[i]]) %in% colnames(multi_comms_i.adj[[i]])]
  adj.i <- adj.i[!duplicated(rownames(adj.i)),!duplicated(colnames(adj.i))] #remove the second DEU (only applies at i=20, when DDR also becomes DEU)
  
  igoecon_sub[[i]] <- adj.i
  
}

saveRDS(directcont_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_weak/directcont_sub_s_weaknodes.rds")
saveRDS(capratio_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_weak/capratio_sub_s_weaknodes.rds")
saveRDS(tradedep_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_weak/tradedep_sub_s_weaknodes.rds")
saveRDS(igosec_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_weak/igosec_sub_s_weaknodes.rds")
saveRDS(igoecon_sub, file = "data/pauls_cranmer/prepped/subsets/strong_nodes/predictors/nodeset_weak/igoecon_sub_s_weaknodes.rds")















# --- Add bridge/membership covariate to outcome nets 

dir <- "data/comm_detection/detected_comms/strong/"
file_list <- list.files(dir, pattern = "strong_membs_weaknodes")


for(k in 1:length(file_list)){

  file.i <- file_list[[k]]
  
  print(k)
  
  membs <- readRDS(paste(dir, file.i, sep = ""))[1971:1990]
  
  
for(i in 1:length(mid_sub)){
  
  names <- network::get.vertex.attribute(mid_sub[[i]], "vertex.names")
  df <- data.frame(bridge = rep(0, length(names)), row.names = names)
  
  temp <- as.data.frame(membs[[i]]) 
  
  for(j in 1:nrow(df)){
    if(rownames(df)[j] %in% rownames(temp)){
      temp.i <- subset(temp, rownames(temp) == rownames(df)[j])
      df[j,] <- ifelse(rowSums(temp.i)>1, 1, 0)
    }else{}
  }
  
  df2 <- data.frame(comm = rep(0, length(names)), row.names = names)
  for(z in 1:nrow(df2)){
    if(rownames(df2)[z] %in% rownames(temp)){
      temp.i <- subset(temp, rownames(temp) == rownames(df2)[z])
      df2[z,] <- ifelse(rowSums(temp.i)==1,as.numeric(colnames(temp.i)[apply(temp.i,1,which.max)]), 0)
    }else{}
  }
  
  network::set.vertex.attribute(mid_sub[[i]], "strong_bridge", df[,1]) # bridge across at least two communities
  network::set.vertex.attribute(mid_sub[[i]], "strong_comm", df2[,1]) # comm assignment if in only one comm
}

  saveRDS(mid_sub, file = paste("data/pauls_cranmer/prepped/subsets/strong_nodes/outcome_nets/mid_sub_s_",gsub("strong_membs_", "", file.i), sep = ""))
  
}



